<?php

/**
 * @copyright   FOSP
 * @version     20120411
 * @todo        Mysql access table rating_player
 * @name        Fpt_Business_RatingPlayer_Adapter_Mysql
 * @author      QuangTM 
 */
class Fpt_Business_RatingPlayer_Adapter_Mysql extends Fpt_Business_RatingPlayer_Adapter_Abstract
{

    /**
     * Get Fpt_Business_RatingPlayer_Adapter_Mysql's instance
     * @return Fpt_Business_RatingPlayer_Adapter_Mysql
     * @author QuangTM
     */
    public static function getInstance()
    {
        if (!isset(self::$instance) || !self::$instance instanceof self)
            self::$instance = new self;
        return self::$instance;
    }

    /**
     * Add user rated for player and match
     * @param array $params
     * @return boolean 
     * @author QuangTM
     */
    public function addRatedPlayer($params)
    {
        try
        {
            // Get DB Obj
            $dbObj = Fpt_Global::getSportSlaveDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_sport_insertRatingPlayer(:p_matchid, :p_teamid, :p_playerid, :p_userid, :p_rating);');

            // Bind param
            $stmt->bindParam('p_matchid', $params['MatchID'], PDO::PARAM_INT);
            $stmt->bindParam('p_teamid', $params['TeamID'], PDO::PARAM_INT);
            $stmt->bindParam('p_playerid', $params['PlayerID'], PDO::PARAM_INT);
            $stmt->bindParam('p_userid', $params['UserID'], PDO::PARAM_INT);
            $stmt->bindParam('p_rating', $params['Rating'], PDO::PARAM_INT);

            // Execute
            $stmt->execute();

            // Get result
            $result = $stmt->fetchColumn();

            // Close cursor
            $stmt->closeCursor();

            // Release
            unset($stmt);

            return $result;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

    /**
     * Check user is rated on match
     * @param int $userID
     * @param int $matchID
     * @return int|boolean 
     * @author QuangTM
     */
    public function isRated($userID, $matchID)
    {
        try
        {
            // Get DB Obj
            $dbObj = Fpt_Global::getSportSlaveDB();

            // Prepare SQL
            $stmt = $dbObj->prepare('CALL sp_sport_checkUserRatingMatch(:p_matchid, :p_userid, @p_result);');

            // Bind param
            $stmt->bindParam('p_matchid', $matchID, PDO::PARAM_INT);
            $stmt->bindParam('p_userid', $userID, PDO::PARAM_INT);

            // Execute
            $stmt->execute();

            // Close cursor
            $stmt->closeCursor();

            $stmt = $dbObj->query("SELECT @p_result");

            // Get result
            $result = $stmt->fetchColumn();

            // Release
            unset($stmt);

            if (intval($result) == 0)
            {
                return FALSE;
            }
            return $result;
        }
        catch (Exception $ex)
        {
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
            return FALSE;
        }
    }

}